package com.liusha.quicksand.service;

import com.liusha.quicksand.dao.CommentRepository;
import com.liusha.quicksand.po.Comment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 一世长安一世欢
 * @Date: 2020/10/23
 * @Time: 17:18
 * @Description: No Description
 */

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {

        Sort sort = Sort.by(Sort.Direction.ASC, "createtime");

        List<Comment> comments = commentRepository.findByBlog_BlogIdAndParentCommentNull(blogId, sort);

        return eachComment(comments);
    }

    @Transactional
    @Override
    public Comment saveComment(Comment comment) {

        Long parentCommentId = comment.getParentComment().getCommentId();

        if (parentCommentId != -1) {
            comment.setParentComment(commentRepository.findById(parentCommentId).get());
        } else {
            comment.setParentComment(null);
        }

        comment.setCreatetime(new Date());

        return commentRepository.save(comment);
    }

    @Override
    public Page<Comment> listComment(Pageable pageable) {

        System.out.println(pageable);

        return commentRepository.findAll(pageable);
    }

    @Override
    public Page<Comment> listComm(String kws, Pageable pageable) {

        System.out.println(kws);
        System.out.println(pageable);

        return commentRepository.findByKeyWords(kws,pageable);
    }

    @Override
    public void deleteById(Long commentId) {

        commentRepository.deleteById(commentId);
    }

    private List<Comment> eachComment(List<Comment> comments) {

        List<Comment> commentsView = new ArrayList<>();
        for (Comment comment : comments) {
            Comment c = new Comment();
            BeanUtils.copyProperties(comment, c);
            commentsView.add(c);
        }

        combineChildren(commentsView);
        return commentsView;
    }

    private void combineChildren(List<Comment> comments) {

        for (Comment comment : comments) {
            List<Comment> replys1 = comment.getReplyComments();
            for (Comment reply1 : replys1) {
                recursively(reply1);
            }

            comment.setReplyComments(tempReplys);

            tempReplys = new ArrayList<>();
        }
    }

    private List<Comment> tempReplys = new ArrayList<>();

    private void recursively(Comment comment) {
        tempReplys.add(comment);
        if (comment.getReplyComments().size() > 0) {
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplyComments().size() > 0) {
                    recursively(reply);
                }
            }
        }
    }
}
